How to create user-defined role in SQL Server Database?
How to create user-defined role in SQL Server Database?
357
16-Jul-2024
Updated on 16-Jul-2024
Ashutosh Kumar Verma
16-Jul-2024SQL Server User-Defined Role
The following example creates a new user and role, grants permissions to the role, and adds a user to the role.
First, set the current database to
masterand create a new login calledkrishnaSecond, switch the current database to
MyCollegeDband create a new user calledkrishnafor login,Create Role
Create a new role called
emp_reportin theMyCollegeDbdatabaseIn this example, we use the
CREATE ROLEstatement to create a new role in theMyCollegeDbdatabase. Theemp_reportis the role name.Grant Permission to the Role
Grant the
SELECTprivilege on thedboschema to theemp_reportAdd User to the Role
Add the
krishnauser to theemp_reportroleFinally, connect to the
MyCollegeDbdatabase using userkrishna. In this case, userkrishnacan only view tables in thedboschema. Also, userkrishnacan only select data from tables in thisdboschema because the user is a member ofemp_reportwhich has theSELECTprivilegeLet's try to update the records in the above table using the SQL query to verify the granted permission,
In the above case the SQL Server denied permission to update the data.
Also, Read: Describe the different types roles in SQL Server.